import { Button } from 'antd'
import { Link } from 'react-router-dom'
import axios from 'axios'
import { isAuth } from '../../helpers/auth'
import { API } from '../../config'

function PayButton({ address, cart, totalPrice }) {
  const handlePay = () => {
    axios.post(`${API}/alipay`, {
      // 支付宝接口要求的必填参数
      "totalAmount": totalPrice,
      "subject": "测试订单标题",
      "body": "测试订单描述",
      // 服务器端要求的必填参数
      "products": cart.map(product=>({product:product._id,count:product.count})),
      "address": address,
      "userId": isAuth().user._id
    }).then(res=>{
      window.location.href  = res.data.result
    })
  }
  const showButton = () => {
    return isAuth() ? <Button onClick={handlePay}>提交</Button> : <Button><Link to="/signin">登录</Link></Button>
  }
  return (
    <>{showButton()}</>
  )
}

export default PayButton